<?php
/**
 * huhaiqin,2024.10.12
 * 游戏预约逻辑部分
 */
namespace Game\Model;
use Think\Model;

class GameYuyueModel extends Model{

    /**
     * 检测权限
     */
    private function _check_auth($check_role = 'admin'){
        if(!$_SESSION['loginUser']){
            _apiReturn(0,'请先登录~');
        }
        if($check_role == 'admin'){
            if($_SESSION['loginUser']['role'] != 'admin'){
                _apiReturn(0,'您没有权限执行该操作~');
            }
        }
    }

    /**
     * huhaiqin
     * 根据ID获取预约详细信息
     */
    public function _find($id = 0){
        if(!$id){
            _apiReturn(0,'参数不合法~');
        }
        $sql = "SELECT A.*,B.`username`,B.`realname`,B.avatar,B.role FROM mini_game_yuyue A LEFT JOIN mini_users B ON A.`uid` = B.`id` WHERE A.id = ".$id." limit 1";
        $data = $this->query($sql);
        $result = $data[0];
        $result['imglist'] = json_decode($result['imglist'],true);
        return $result;
    }

    /**
     * huhaiqin
     * 添加、编辑预约
     */
    public function _edit($post=[]){
        $this->_check_auth();
        if($post['title'] == ''){
            _apiReturn(0,'预约标题不能为空~');
        }
        if($post['imglist_web']){
            $tmpImgList = [];
            for($i = 0 ; $i < count($post['imglist']);$i++){
                $tmpImgList[$i] = json_decode($post['imglist'][$i],true);
            }
            $post['imglist'] = json_encode($tmpImgList,JSON_UNESCAPED_UNICODE);
        }else{
            $post['imglist'] = json_encode($post['imglist'],JSON_UNESCAPED_UNICODE);
        }
        $post['update_time'] = time();
        $data = $this->create($post);
        $data['uid'] = $post['uid'] ? $post['uid'] : $_SESSION['loginUser']['id']; //用户ID优先使用post过来的，默认使用当前登录账户
        if(!$data['uid']){
            _apiReturn(0,'请先登录~');
        }
        if($data['id']){
            $checkUser = D('Users')->field('id,role')->find($data['uid']);
            if($checkUser['role']!='admin'){
                unset($data['uid']); //管理员有权限改变数据归属
                //需要检测是否有权限修改
                $map = ['uid'=>$checkUser['id'],'id'=>$data['id']];
                $check = $this->where($map)->find();
                if(!$check){
                    _apiReturn(0,'对不起，只有创建者或管理员有权限编辑~');
                }
            }
            if($this->save($data)){
                _apiReturn(1,'success',$data);
            }else{
                _apiReturn(0,'修改失败~');
            }
        }else{
            $data['create_time'] = time();
            $data['id'] = $this->add($data);
            if($data['id']){
                _apiReturn(1,'success',$data);
            }else{
                _apiReturn(0,'添加失败~');
            }
        }
    }

    /**
     * huhaiqin
     * 数据列表接口
     * param $p int 当前页码
     * param $row int 一页记录数
     * param $query string 查询的sql语句
     */
    public function _query($p = 1 , $row = 10 , $query = ''){
        $this->_check_auth();
        $sql = "SELECT A.*,B.`username`,B.`realname`,B.avatar,B.role FROM mini_game_yuyue A LEFT JOIN mini_users B ON A.`uid` = B.`id` WHERE 1=1 ";
        $sqlcount = "SELECT COUNT(DISTINCT A.`id`) AS tp_count FROM mini_game_yuyue A LEFT JOIN mini_users B ON A.`uid` = B.`id` WHERE 1=1 ";
        if($query){
            $sql .= $query;
            $sqlcount .= $query;
        }
        //order
        $sql .= " ORDER BY A.listorder asc,A.id desc";
        $sql .= " LIMIT ".($p-1) * $row.",".$row;
        $data['data'] = $this->query($sql);       
        $total = $this->query($sqlcount);
        $data['page']['total'] = $total[0]['tp_count'];
        //处理删除最后一条数据的时候没有数据的bug,请求最后一页没有数据的bug
        if($data['page']['total']>0){
            if (floor(($data['page']['total'] + $row - 1) / $row) < $p) {
                $p = $p - 1;
            }
        }
        //页数数据
        $data['page']['count'] = (int)(($data['page']['total'] + $row -1) / $row);
        $data['page']['cur'] = $p;
        $data['page']['row'] = $row;
        // $data['sql'] = $sql;
        return $data;
    }
}